In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
#import seaborn as sns
%matplotlib inline
#np.random.seed(2)
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools
from keras.utils.np_utils import to_categorical
from keras.models import Model
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, Input
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from keras import backend as K
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#sns.set(style='white', context='notebook', palette='deep')
In [2]:
train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")
In [3]:
y_train = train['label']
X_train = train.drop(labels = ["label"],axis = 1)
X_train = X_train / 255.0
test = test / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
test = test.values.reshape(-1,28,28,1)
y_train = to_categorical(y_train, num_classes = 10)
In [4]:
random_seed = 2
X_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=random_seed)
In [5]:
Y_train.shape
Out[5]:
In [6]:
def build_nn(input_shape, output_shape):
input_tensor = Input(shape=input_shape)
out = Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', activation ='relu')(input_tensor)
out = Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', activation ='relu')(out)
out = MaxPooling2D(pool_size=(2,2))(out)
out = Dropout(0.25)(out)
out = Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu')(out)
out = Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu')(out)
out = MaxPooling2D(pool_size=(2,2), strides=(2,2))(out)
out = Dropout(0.25)(out)
out = Flatten()(out)
out = Dense(256, activation = "relu")(out)
out = Dropout(0.5)(out)
output_tensor = Dense(output_shape, activation = "softmax")(out)
return input_tensor, output_tensor
In [8]:
input_shape = X_train[0].shape
output_shape = 10
input_tensor, output_tensor = build_nn(input_shape, output_shape)
model = Model(inputs=input_tensor, outputs=output_tensor)
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1, factor=0.5, min_lr=0.00001)
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=10, # randomly rotate images in the range (degrees, 0 to 180)
zoom_range = 0.1, # Randomly zoom image
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=False, # randomly flip images
vertical_flip=False) # randomly flip images
datagen.fit(X_train)
print(model.summary())
In [9]:
history = model.fit(X_train, Y_train, epochs=30, batch_size=256, validation_data=(X_val, Y_val))
In [12]:
epochs = 30
batch_size = 128
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data = (X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction])
In [ ]:
In [10]:
results = model.predict(test)
results = np.argmax(results,axis = 1)
results = pd.Series(results,name="Label")
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
submission.to_csv("submissions.csv",index=False)
In [ ]: